begintownscript;

variables;
short ticks,intheway;

body;

beginstate INIT_STATE;
	if (get_flag(2,2)==1)
	{
		set_incidental_sound(1); // is it spooky yet? turn off the normal music
		play_sound(-150); // and play spooky music
		ticks=0;
	}
	else set_incidental_sound(0);	// else, turn it on
break;

beginstate START_STATE;
	// incidental sounds can't be changed. We'll have to improvise.
	if (get_flag(2,2)==1)
	{
		ticks=ticks+1;
		if (get_ran(3,1,6)==18 && ticks>200) 
		{	
			play_sound(-150);
			ticks=0;
		}
		//else print_big_str("",ticks,"");
	}
break;

beginstate EXIT_STATE;
break;

beginstate 10;
	if (get_flag(2,6)==1) end();
	message_dialog("The air is slightly cooler down here. You also immediately notice that the sharp stench of goblin habitation is gone. They must not have moved in here for some reason.",
	"Instead, you smell faint traces of decay, but it is not particularly strong. Of course the dry air would tend to mummify rather than decompose any corpses.");
	set_flag(2,6,1);
break;

beginstate 11;  // stairway back up
	print_str("You walk back up the sloping tunnel.");
	move_to_new_town(1,38,8);
break;

beginstate 12; // approach wall
	if (get_flag(2,0)==1) end();
	message_dialog("The tunnel ahead has been walled off with crude blocks of stone. It appears to be goblin workmanship, and done in great haste at that. The gate set in it is crude, but made of strong iron bands. You won't get it open without a key.","");
	set_flag(2,0,1);
break;

beginstate 13; // walk down tunnel
	if (get_flag(2,2)==1) end();
	message_dialog("The air seems a bit cooler here behind the wall. Even chilly. The utter silence in this tunnel gains an almost menacing edge.","");
	set_flag(2,2,1);
	set_incidental_sound(1);
	play_sound(-150);
break;

beginstate 14; // enter secret chamber
	if (get_flag(2,3)==1) end();
	message_dialog("How odd. A fake cave wall is hiding a passage here. You step through it and into a very strange scene: Four crystal pillars are standing in the corners, shining beams of light onto a platform in the center.",
			"A goblin is running straight toward you, screaming. You draw your weapons, but he passes right through you and disappears. It must have been some strange projection.");
	set_flag(2,3,1);
break;

beginstate 15; 
	if (has_special_item(2)>0) end();
	reset_dialog();
	add_dialog_str(0,"As you approach the pentangle that is inlaid in the rough basalt floor, you see something lying on the ground, in the very center of the five-pointed star.",0);
	add_dialog_str(1,"It is the last thing you expected to find here. And yet, it is all you ever wanted to find.",0);
	add_dialog_str(2,"On the side of the small leather satchel, the silver-winged crown of the Imperial Post shimmers softly. It is your lost courier pouch.",0);
	add_dialog_str(3,"Why would it end up here, of all places, where even the goblins do not go? Still, there is nothing to do but pick it up and deliver it now...",0);
	add_dialog_choice(0,"Leave it lying");
	add_dialog_choice(1,"Pick it up");
	if (run_dialog(1)==2) {
		change_spec_item(2,1);
		toggle_quest(1,0);
	}
break;

beginstate 16;
	put_straight_zap(17,10,13,8,4);
	put_straight_zap(17,9,13,8,4);
	put_straight_zap(18,10,13,8,4);
	put_straight_zap(18,9,13,8,4);
	activate_hidden_group(2);
	put_sparkles_on_space(13,8,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	if (get_flag(2,5)==0) message_dialog("Someone walks through the cave wall. It's Kernighan, the priest from the inn!",
			"However, he is translucent and oblivious to your presence as he walks into the room. It seems to be yet another magical recording, though why it is here, you have no idea. You can but watch.");
	intheway=char_on_loc(16,9);
	relocate_character(11,14,8);
	put_sparkles_on_space(14,8,11,6);
	force_instant_terrain_redraw();

	run_animation();
	pause(4);
	relocate_character(11,15,9);
	force_instant_terrain_redraw();
	put_sparkles_on_space(15,9,11,6);
	run_animation();
	pause(4);
	relocate_character(11,16,9);
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(4);
	if (intheway> -1) 
	{
		message_dialog("Obliviously, the magical image walks right through you. The chill and nausea are unbearable, and you quickly back away.","");
		relocate_character(intheway,19,10);
		text_bubble_on_char(intheway,"Eeeeeew"); //:P
		force_instant_terrain_redraw();
		pause(10);
		text_bubble_on_char(intheway,"");
		force_instant_terrain_redraw();
	}
	text_bubble_on_char(11,"Okay, now that's taken care of.");
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(15);
	text_bubble_on_char(11,"");
	text_bubble_on_char(11,"They'll never find it here.");
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(15);
	text_bubble_on_char(11,"");
	text_bubble_on_char(11,"Oh, silly. Of course they will.");
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(15);
	text_bubble_on_char(11,"");
	text_bubble_on_char(11,"And when they deliver this fake...");
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(11,"");
	text_bubble_on_char(11,"I just wish I could see their faces.");
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(15);
	text_bubble_on_char(11,"");
	force_instant_terrain_redraw();
	pause(5);
	if (get_flag(2,5)==0) message_dialog("The Kernighan-recording takes out a leather bag and puts it into the pentangle.","");
	set_character_facing(11,2);
	force_instant_terrain_redraw();
	put_sparkles_on_space(16,9,11,6);
	run_animation();
	pause(4);
	relocate_character(11,15,9);
	force_instant_terrain_redraw();
	put_sparkles_on_space(15,9,11,6);
	run_animation();
	pause(4);
	relocate_character(11,14,8);
	force_instant_terrain_redraw();
	put_sparkles_on_space(14,8,11,6);
	run_animation();
	pause(4);
	relocate_character(11,13,8);
	force_instant_terrain_redraw();
	put_sparkles_on_space(13,8,11,6);
	run_animation();
	pause(4);
	erase_char(11);
	force_instant_terrain_redraw();
	pause(5);
	if (get_flag(2,5)==0) message_dialog("You're confused. Did Kernighan steal your message? Why did he mean for you to find it, and what was he implying? You sense that there is still a riddle here.","");
	set_flag(2,5,1);
	set_flag(2,8,0); // set it back to the goblins.
break;

beginstate 17;
	if (get_flag(2,7)==1) end();
	message_dialog("Humanoid skeletons are strewn all over the cave floor here. The goblins must be using this cavern as a graveyard. Perhaps that is why they're not living here.",
			"Or perhaps there's something else.");
	set_flag(2,7,1);
break;

beginstate 18;
	block_entry(1);
	if (get_flag(2,4)>0 && get_flag(2,4)<5)
	{
		message_dialog("The control panel is shut down. Apparently the projector is still running, and cannot generate another projection while the current one is in place.","");
		end();
	}
	reset_dialog();
	add_dialog_str(0,"There is a mess of buttons on this control panel. Some of them are lit up.",0);
	add_dialog_choice(0,"Leave alone.");
	add_dialog_choice(1,"Press a few.");
	if (run_dialog(0)==1) end();
	print_str_color("You push a few of the buttons. The machine hums.",2);
	if (get_flag(2,8)==1 && get_flag(2,4)==0) set_state_continue(20);
	put_straight_zap(20,6,17,10,6);
	put_straight_zap(20,13,17,9,3);
	put_straight_zap(15,6,18,10,0);
	put_straight_zap(15,13,18,9,5);
	put_effect_on_space(17,10,4,6,1);
	put_effect_on_space(17,9,4,6,1);
	put_effect_on_space(18,10,4,6,1);
	put_effect_on_space(18,9,4,6,1);
	play_sound(-162);
	run_animation();
	// okay, the projector is now powered up. Now show one of two cutscenes or (only once) a combat scene.
	// present state indicated by sdf 2/8.
	if (get_flag(2,8)==0)
	{
		set_state_continue(19); // show the goblins again.
	}
	else
	{
		set_state_continue(16); // show kernighan.
	}
break;

beginstate 19;
	activate_hidden_group(3);
	force_instant_terrain_redraw();
	// put_straight_zap(17,10,19,9,4);
	put_straight_zap(17,9,19,9,4);
	put_straight_zap(18,10,19,9,4);
	put_straight_zap(18,9,19,9,4);
	put_sparkles_on_space(19,9,11,6);
	
	put_straight_zap(17,10,14,7,4);
	put_straight_zap(17,9,14,7,4);
	put_straight_zap(18,10,14,7,4);
	//	put_straight_zap(18,9,14,7,4);	
	put_sparkles_on_space(14,7,11,6);

	put_straight_zap(17,10,18,10,4);
	put_straight_zap(17,9,18,10,4);
	// 	put_straight_zap(18,10,18,10,4);
	//	put_straight_zap(18,9,18,10,4);
	put_sparkles_on_space(18,10,11,6);
	run_animation();
	pause(10);
	if (get_flag(2,9)==0) message_dialog("You jump briefly. Beams criss-cross from the blue crystal columns to the platform on the ground and through the air, and several goblin shapes appear.",
					"You realize what's happening: This is the same recording as the one that was running when you entered the cave.");
	
	text_bubble_on_char(25,"Look here, Ogluk!");
	put_sparkles_on_space(19,9,11,6);
	put_sparkles_on_space(14,7,11,6);
	put_sparkles_on_space(18,10,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	text_bubble_on_char(25,"");
	text_bubble_on_char(25,"It's shiny-shiny!");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	text_bubble_on_char(25,"");
	text_bubble_on_char(28,"Leave it alone, Gobrak!");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	text_bubble_on_char(28,"");
	text_bubble_on_char(28,"Remember what mom said!");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	text_bubble_on_char(28,"");
	text_bubble_on_char(26,"Whaddaya havey there? Lemme look!");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(10);
	relocate_character(26,15,7);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(4);
	text_bubble_on_char(26,"");
	relocate_character(26,16,8);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(4);
	text_bubble_on_char(25,"Wonder what this does...");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	text_bubble_on_char(25,"");
	text_bubble_on_char(28,"No!");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(15);
	play_sound(-37);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	text_bubble_on_char(28,"");
	force_instant_terrain_redraw();
	run_animation();
	pause(5);
	play_sound(-162);
	pause(7);
	play_sound(-7);
	put_straight_zap(20,6,17,10,6);
	put_straight_zap(20,13,17,9,3);
	put_straight_zap(15,6,18,10,0);
	put_straight_zap(15,13,18,9,5);
	put_effect_on_space(17,10,4,6,1);
	put_effect_on_space(17,9,5,6,1);
	put_effect_on_space(18,10,5,6,1);
	put_effect_on_space(18,9,4,6,1);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(28,11,6);
	run_animation();
	
	activate_hidden_group(4);
	force_instant_terrain_redraw();

	put_straight_zap(17,10,char_loc_x(27),char_loc_y(27),4);
	put_straight_zap(17,9,char_loc_x(27),char_loc_y(27),4);
	put_straight_zap(18,10,char_loc_x(27),char_loc_y(27),4);
	put_straight_zap(18,9,char_loc_x(27),char_loc_y(27),4);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(28,11,6);
	run_animation();
	pause(15);
	text_bubble_on_char(26,"BEHIND YOU!");
	set_character_pose(27,1);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(5);
	set_character_facing(25,2);
	text_bubble_on_char(25,"What the-- ");
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(28,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(8);
	text_bubble_on_char(26,"");
	put_jagged_zap(char_loc_x(27),char_loc_y(27),char_loc_x(25),char_loc_y(25),0);
	put_jagged_zap(char_loc_x(27),char_loc_y(27),char_loc_x(28),char_loc_y(28),0);
	put_sparkles_on_char(25,11,6);
	put_sparkles_on_char(26,11,6);
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(28,11,6);
	put_effect_on_char(25,1,3,2);
	put_effect_on_char(28,1,3,2);
	play_sound(-152);
	run_animation();
	set_character_pose(27,0);
	kill_char(25,2,0);
	kill_char(28,2,0);
	
	text_bubble_on_char(26,"AAAAAAAHHHH!");
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(26,11,6);
	force_instant_terrain_redraw();
	run_animation();
	pause(5);

	set_character_facing(26,2);
	force_instant_terrain_redraw();
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(26,11,6);
	run_animation();

	relocate_character(26,15,8);
	force_instant_terrain_redraw();
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(26,11,6);
	run_animation();
	
	relocate_character(26,14,8);
	force_instant_terrain_redraw();
	put_sparkles_on_char(27,11,6);
	put_sparkles_on_char(26,11,6);
	run_animation();

	relocate_character(26,13,8);
	erase_char(27);
	force_instant_terrain_redraw();
	put_sparkles_on_char(26,11,6);
	run_animation();
	erase_char(26);
	erase_char(27);
	put_stain_on_space(19,9,-1);
	put_stain_on_space(18,10,-1);
	force_instant_terrain_redraw();

	set_flag(2,9,1); // now we've seen the goblins.
	set_flag(2,8,1); // switch the projector to the next setting
break;

beginstate 20;

	put_straight_zap(20,6,17,10,6);
	put_straight_zap(20,13,17,9,3);
	put_straight_zap(15,6,18,10,0);
	put_straight_zap(15,13,18,9,5);
	put_effect_on_space(17,10,4,6,1);
	put_effect_on_space(17,9,5,6,1);
	put_effect_on_space(18,10,5,6,1);
	put_effect_on_space(18,9,4,6,1);
	play_sound(-162);
	pause(7);
	play_sound(-7);
	run_animation();

	activate_hidden_group(1);
	force_instant_terrain_redraw();
	put_straight_zap(17,10,17,6,4);
	put_straight_zap(17,9,17,6,4);
	put_straight_zap(18,10,20,8,4);
	put_straight_zap(17,10,20,11,4);
	put_straight_zap(17,9,15,11,4);
	put_straight_zap(18,10,20,11,4);
	put_straight_zap(18,9,15,11,4);
	put_straight_zap(18,9,15,8,4);
	run_animation();
	pause(15);
	message_dialog("The projector powers up and generates several figures in the air around you. At first you believe you are in another recording. But these creatures seem uncomfortably real.","");
	set_flag(2,4,1);
	set_flag(2,8,2);
break;